package zhixin.codetran.dffwgweb;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @program: java-control-browser
 * @create: 2022-05-07 16:02
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        System.setOut(new PrintStream(new FileOutputStream("输出.jsp")));
        String html = FileUtils.readFileToString(new File("C:\\CODE\\JAVA\\apache-tomcat-8.0.43\\webapps\\dffgw-web\\WEB-INF\\jsp\\attachment\\attachment-lists.jsp"), StandardCharsets.UTF_8);
        Document parse = Jsoup.parse(html);
        Element showtable = parse.getElementsByClass("showtable").first();
        if(showtable == null) {
            throw new RuntimeException("表格为找到");
        }
        Element tr = showtable.getElementsByTag("tr").first();
        if(tr == null) {
            throw new RuntimeException("表格为找到");
        }
        Element td = tr.getElementsByTag("td").first();
        if(td == null) {
            throw new RuntimeException("表格为找到");
        }
        List<Element> divs = td
                .children()
                .stream()
                .filter(i -> "div".equals(i.tagName()))
                .collect(Collectors.toList());
        Consumer<Elements> removeStyle = (elements)->{
            elements.forEach(i->{
                i.removeAttr("style");
            });
        };
        for (int i = 0; i < divs.size(); i++) {
            Element div = divs.get(i);
            Element p = div.getElementsByTag("p").first();
            Element span = div.getElementsByTag("span").first();
            if (p != null || span != null) {
                String text = null;
                if(p != null) {
                    text = p.text();
                }
                if(span != null) {
                    text = span.text();
                }
                String pattern = "[\\u4e00-\\u9fa5]+";
                Pattern r = Pattern.compile(pattern);
                Matcher m = r.matcher(text);
                if (m.find()) {
                    String group = m.group();
                    int length = group.length();
                    Elements inputs = div.getElementsByTag("input");
                    inputs.addAll(div.getElementsByTag("select"));
                    for (int i1 = 0; i1 < inputs.size(); i1++) {
                        Element input = inputs.get(i1);
                        input.removeAttr("style");
                        if(group.contains("时间")) {
                            if(inputs.size()>1) {
                                input.addClass("time_label4_input_append1");
                            } else {
                                input.addClass("time_label4_input");
                            }
                        } else {
                            if (inputs.size() > 1) {
                                if (i1 == 0) {
                                    input.addClass("select_label" + length + "_input_append3");
                                } else {
                                    input.addClass("select_label" + length + "_input_append2");
                                }
                            } else {
                                input.addClass("select_label" + length + "_input");
                            }
                        }
                    }
                }
            } else {
                if(i == 0) {
                    Element element = div.children().get(0);
                    element.removeAttr("style");
                    element.addClass("input_nolabel");
                } else {
                    Elements children = div.children();
                    if(!children.isEmpty()) {
                        div.remove();
                        div.removeAttr("style");
                        Elements as = div.getElementsByTag("a");
                        removeStyle.accept(as);
                        Elements inputs = div.getElementsByTag("input");
                        removeStyle.accept(inputs);
                        Elements buttons = div.getElementsByTag("button");
                        removeStyle.accept(buttons);
                        Element td1 = tr.appendElement("td");
                        if(as.size() == 2) {
                            td1.addClass("btn_a_a");
                        }
                        if(as.size() == 1 && inputs.size() == 1) {
                            td1.addClass("btn");
                        }
                        td1.appendChild(div);
                    }
                }
            }
        }
        System.out.println(tr);
    }
}
